﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EislaufScb.Model.Repository;
using EislaufScb.Model;
using EislaufScb.Model.Repository.Organisation;

namespace EislaufScb.WebForms.ViewModel.Person
{
    public class SkaterViewModel : PersonViewModel
    {
        public List<string> Certifications { get; set; }

        public string PersonIsTrainedBy { get; set; }

        public SkaterViewModel()
        {
            Certifications = new List<string>();
        }

        public static List<SkaterViewModel> GetSkaters()
        {
            List<SkaterViewModel> result = new List<SkaterViewModel>();

            using (PersonRepository repository = new PersonRepository(new ClubModelContainer()))
            {
                List<EislaufScb.Model.Person> skaters = repository.GetPersonByType("Skater");

                foreach (var skater in skaters)
                {
                    SkaterViewModel person = new SkaterViewModel();
                    person.FirstName = skater.FirstName;
                    person.MiddleName = skater.MiddleName;
                    person.LastName = skater.LastName;
                    person.DateOfBirth = skater.DateOfBirth.HasValue ? skater.DateOfBirth.Value.ToShortDateString() : string.Empty;
                    person.PersonIsTrainedBy = skater.TrainedBy;

                    string certification = string.Empty;
                    skater.Test.ToList().ForEach(s => certification += string.Format(@"{0}<BR/>", s.Description));

                    person.Certifications = new List<string>();

                    foreach (var cert in skater.Test)
                    {
                        person.Certifications.Add(cert.Description);
                    }

                    result.Add(person);
                }
            }

            return result;
        }

        public static SkaterViewModel GetSkaterDetails(string firstName, string lastName)
        {
            PersonRepository repository = new PersonRepository(new ClubModelContainer());
            SkaterViewModel result = new SkaterViewModel();

            using (repository)
            {
                EislaufScb.Model.Person person = repository.GetPersonByFirstNameAndLastName(firstName, lastName).FirstOrDefault();

                result.FirstName = person.FirstName;
                result.MiddleName = person.MiddleName;
                result.LastName = person.LastName;
                result.PersonIsTrainedBy = person.TrainedBy;

                foreach (var cert in person.Test)
                {
                    result.Certifications.Add(cert.Description);
                }

                return result;
            }
        }
    }
}